<template></template>

<script>
let rewardedVideoAd = null;
export default {
	props: {
		nowAdTypeKs: {
			type: String,
			default: ''
		}
	},
	data() {
		return {};
	},
	methods: {
		async isShowAd() {
			console.log('isShowAd');
			this.$statisAd({
				action: 'REQUEST',
				adId: this.nowAdTypeKs === 'iaaAd' ? 100024593 : 100014857,
				adSlot: 2
			});

			if(!rewardedVideoAd) {
				rewardedVideoAd = await uni.createRewardedVideoAd({
					type: 100011056,
					unitId: this.nowAdTypeKs === 'iaaAd' ? 100024593 : 100014857
				});
			}
			rewardedVideoAd.show().then(() => {
				this.$statisAd({
					action: 'EXPOSURE',
					adId: this.nowAdTypeKs === 'iaaAd' ? 100024593 : 100014857,
					adSlot: 2
				});
			}).catch(() => {
				rewardedVideoAd
					.load()
					.then(() => {
						uni.hideLoading()
						rewardedVideoAd.show()
						this.$statisAd({
							action: 'EXPOSURE',
							adId: this.nowAdTypeKs === 'iaaAd' ? 100024593 : 100014857,
							adSlot: 2
						});
					})
					.catch(err => {
						uni.hideLoading()
						uni.showToast({
							title: "广告加载失败,请稍后再试。。",
							icon: 'none'
						})
						setTimeout(() => {
							this.$emit('closeAdBox', true)
						}, 3000)
						console.log('激励视频广告显示失败', err);
					});
			});
			this.$uma && this.$uma.trackEvent('Ume_advertisement', {
				'Ume_adv_button_click': '广告解锁按钮点击'
			});
		}
	},
	onReady() {
		console.log('onReady27');
		rewardedVideoAd = uni.createRewardedVideoAd({
			type: 100011056,
			unitId: this.nowAdTypeKs === 'iaaAd' ? 100024593 : 100014857
		});
		console.log(rewardedVideoAd)
		rewardedVideoAd.onLoad(loadCode => {
			console.log('onLoad event emit', loadCode);
		});
		rewardedVideoAd.onError(({ errCode }) => {
			uni.hideLoading()
			uni.showToast({
				title: "广告加载失败,请稍后再试。。",
				icon: 'none'
			})
			setTimeout(() => {
				this.$emit('closeAdBox', true)
			}, 3000)
		});
		rewardedVideoAd.onClose(({ isEnded }) => {
			getApp().globalData.adHasSee += 1;
			this.$uma && this.$uma.trackEvent('Ume_advertisement', {
				Ume_adv_close: '广告关闭按钮点击量'
			});

			if (isEnded) {
				this.$uma && this.$uma.trackEvent('Ume_advertisement', {
					Ume_adv_clear_success: '广告解锁成功'
				});
				this.$emit('closeAdBoxAndUnlock', true)
			} else {
				this.$emit('closeAdBox', true)
				this.$uma && this.$uma.trackEvent('Ume_advertisement', {
					Ume_adv_defeated: '广告拉取后未观看关闭'
				});
			}
			// this.$emit('adClose', isEnded);
		});
	},
	onUnload() {
		rewardedVideoAd.destroy();
		rewardedVideoAd = null;
	}
};
</script>

<style></style>
